package edu.stanford.bmir.protege.web.client.ui.ontology.scene;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.gwtext.client.widgets.MessageBox;
import com.hp.hpl.jena.graph.query.SimpleQueryEngine.Cons;

import edu.stanford.bmir.protege.web.client.project.Project;
import edu.stanford.bmir.protege.web.client.rpc.data.layout.ProjectLayoutConfiguration;
import edu.stanford.bmir.protege.web.client.rpc.data.layout.TabConfiguration;
import edu.stanford.bmir.protege.web.client.ui.ProjectDisplayImpl;
import edu.stanford.bmir.protege.web.client.ui.generated.UIFactory;
import edu.stanford.bmir.protege.web.client.ui.ontology.scene.service.SceneService;
import edu.stanford.bmir.protege.web.client.ui.ontology.scene.service.SceneServiceAsync;
import edu.stanford.bmir.protege.web.client.ui.portlet.EntityPortlet;
import edu.stanford.bmir.protege.web.client.ui.tab.AbstractTab;
import edu.stanford.bmir.protege.web.client.ui.tab.UserDefinedTab;

public class BaseScene extends Composite {
	
	public final static SceneServiceAsync service = GWT.create(SceneService.class);
	
	protected Project project ;
	
	protected ProjectDisplayImpl pdp;
	
	private List<EntityPortlet> plst = new ArrayList<EntityPortlet>();
	
	public BaseScene ( Project project ) {
		this.project = project;
		pdp = ProjectCache.maps.get( project.getProjectId() );
	}
	
	protected void addPortlet ( String name ) {
		
		if ( "".equals( name ) ) {
			MessageBox.alert("暂未集成该工具！");
		} else {
			onPortletAdded(name);
		}
	}
	
	
	private void onPortletAdded(final String javaClassName) {
        GWT.runAsync(new RunAsyncCallback() {
            @Override
            public void onFailure(Throwable reason) {
                GWT.log("There was a problem adding the portlet", reason);
            }

            @Override
            public void onSuccess() {
            	
                EntityPortlet portlet = UIFactory.createPortlet(project, javaClassName);
                if (portlet == null) {
                    return;
                }
                AbstractTab activeTab = UIFactory.maps.get("key");
                
                for ( EntityPortlet p : plst ) {
                	activeTab.removePortlet(p);
                	plst = new ArrayList<EntityPortlet>();
                }
                activeTab.addPortlet(portlet, activeTab.getColumnCount() - 1);
                plst.add( portlet );
                activeTab.doLayout();
            }
        });

    }
	public void createTab(final String className) {
		service.getProjectLayoutConfiguration( new AsyncCallback<ProjectLayoutConfiguration>() {

			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(ProjectLayoutConfiguration result) {
				for ( TabConfiguration tabConfig : result.getTabs() ) {
					String name = tabConfig.getName();
					if ( name.equals( className ) ) {
						ProjectDisplayImpl p = ProjectCache.maps.get( project.getProjectId() );
						/*tabConfig.setName(UserDefinedTab.class.getName());
				         
				        UserDefinedTab userDefinedTab = new UserDefinedTab(project);
				        project.getLayoutManager().setupTab(userDefinedTab, tabConfig);
				        project.getProjectLayoutConfiguration().addTab(tabConfig);
				         
				        p.createTab(userDefinedTab);*/
						p.onTabAdded( className,tabConfig );
					}
				}
			}
		});
	}

}
